<template>
  <div class="app-container">
    <div class="filter-container query">
      <el-form :model="listQuery" label-position="right" label-width="100px">
        <el-row :gutter="10">
          <el-col :xs="12" :sm="8" :lg="6">
            <el-form-item label="用户编号" prop="userCode">
              <el-input v-model="listQuery.userCode" @keyup.enter.native="handleFilter" />
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="8" :lg="6">
            <el-form-item label="用户IP" prop="ip">
              <el-input v-model="listQuery.ip" @keyup.enter.native="handleFilter" />
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="8" :lg="6">
            <el-form-item label="标题" prop="title">
              <el-input v-model="listQuery.title" @keyup.enter.native="handleFilter" />
            </el-form-item>
          </el-col>
          <!-- <el-col :xs="12" :sm="8" :lg="6">
            <el-form-item label="操作内容" prop="content">
              <el-input v-model="listQuery.content" @keyup.enter.native="handleFilter" />
            </el-form-item>
          </el-col> -->
          <el-col :xs="12" :sm="8" :lg="6">
            <el-form-item label="操作时间" prop="createAt" @keyup.enter.native="handleFilter">
              <el-date-picker
                v-model="listQuery.createAt"
                type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss"
                range-separator="至"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="8" :lg="6">
            <el-button type="primary" icon="el-icon-search" size="small" @click="handleFilter">
              搜索
            </el-button>
            <el-button type="info" icon="el-icon-refresh" size="small" @click="handleReset">
              重置
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <el-table
      :loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;height: 100%;"
      size="small"
      @sort-change="sortChange"
    >
      <el-table-column label="操作时间" prop="createAt" sortable="custom" width="300" header-align="center" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createAt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标题" prop="title" sortable="custom" width="600" header-align="center" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户编号" prop="userCode" sortable="custom" width="300" header-align="center" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.userCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户IP" prop="ip" sortable="custom" width="300" header-align="center" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.ip }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作内容" prop="content" min-width="100%" header-align="center" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.content }}</span>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageNoSize" size="small" @pagination="getList" />
  </div>
</template>

<script>
import { fetchList } from '@/api/base/timeline'
import Pagination from '@/components/Pagination'

export default {
  name: 'BaseTimeLineQuery',
  components: { Pagination },
  data() {
    return {
      orgLoading: false,
      tableKey: 0,
      list: null,
      total: 0,
      ajaxLoading: false,
      listLoading: false,
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        sortField: undefined,
        sortOrder: undefined,
        userCode: '',
        ip: '',
        title: '',
        content: '',
        createAt: []
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      }, response => {
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    handleReset() {
      this.listQuery = {
        userCode: '',
        ip: '',
        title: '',
        content: '',
        createAt: []
      }
    },
    sortChange(data) {
      const { prop, order } = data
      this.sortByField(prop, order)
    },
    sortByField(prop, order) {
      if (order === 'ascending') {
        this.listQuery.sortField = prop
        this.listQuery.sortOrder = 'asc'
      } else if (order === 'descending') {
        this.listQuery.sortField = prop
        this.listQuery.sortOrder = 'desc'
      } else {
        this.listQuery.sortField = undefined
        this.listQuery.sortOrder = undefined
      }
      this.handleFilter()
    }
  }
}
</script>
